package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.constant.RedisConstant;
import com.itheima.pojo.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import static com.itheima.constant.RedisConstant.GEO_KEY;

/**
 * @author 王同学
 */
@RestController
@RequestMapping("/user/location")
public class LocationController {
    //Location 意思是: 位置
    //导一下redis
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping
    public CommonResult push(@RequestBody Location location) {
        //这个就是传两个参数进来，一个是经度一个是纬度
        //声明一个坐标点
        Point point = new Point(location.getLongitude(), location.getLatitude());
        //写入redis
        redisTemplate.opsForGeo().add(GEO_KEY, point, location.getName());

        return CommonResult.success("我成功了");
    }

    @GetMapping
    public CommonResult getRadius(double longitude,double latitude,double distance){
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs()
                // 包含距离
                .includeDistance()
                // 包含经纬度
                .includeCoordinates()
                // 升序
                .sortAscending()
                // 最大查询数量
                .limit(10);
        //设置圆心 传经纬度
        Point point=new Point(longitude,latitude);
        //设置半径
        Distance distance1=new Distance(distance, Metrics.KILOMETERS);
        //画圆
        Circle circle=new Circle(point,distance1);
        //写入redis
        GeoResults<RedisGeoCommands.GeoLocation<String>> radius = redisTemplate.opsForGeo().radius(GEO_KEY, circle, args);


        return CommonResult.success(radius);
    }
}
